//
//  DestinationViewController.m
//  Journey
//
//  Created by dllo on 16/5/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DestinationViewController.h"
#import "SSNetworking.h"
#import "CountryTableViewCell.h"
#import "HotCountryTableViewCell.h"
#import "DestinationModel.h"
#import "hotCountryModel.h"
#import "countryModel.h"
#import "DestinstionDetailsViewController.h"

#import "YNDataReading.h"
static NSString *const HotCountryCell = @"hotCountryCELL";
static NSString *const countryCell = @"CountryCELL";

@interface DestinationViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)BaseTableView *disTableView;
@property(nonatomic,strong)UIView *HeaderView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *tempHotArray;
@property(nonatomic,strong)NSMutableArray *tempArray;
@property(nonatomic,strong)NSMutableArray *readArray;



@end

@implementation DestinationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0x139a43,0x4F4F4F);
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(150 * WIDTHC, 0, 100 * WIDTHC, 44 * WIDTHC)];
    title.text = @"目 的 地";
    title.textAlignment = NSTextAlignmentCenter;
    title.dk_textColorPicker = DKColorPickerWithRGB(0x1C1C1C,0xE8E8E8);
    
    self.navigationItem.titleView = title;
   // self.navigationItem.hidesBackButton = YES;
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.dataArray = [NSMutableArray array];
    
    //加载条
    //显示
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
     [self getData];
    self.disTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.disTableView];
    self.disTableView.backgroundColor = [UIColor whiteColor];
    self.disTableView.delegate = self;
    self.disTableView.dataSource = self;
    //注册
    [self.disTableView registerClass:[CountryTableViewCell class] forCellReuseIdentifier:countryCell];
    [self.disTableView registerClass:[HotCountryTableViewCell class] forCellReuseIdentifier:HotCountryCell];
    self.disTableView.showsVerticalScrollIndicator = NO;
    //头视图
    self.HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 267 * WIDTHC)];
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map"]];
    image.frame = CGRectMake(0, 0, WIDTH, 267 * WIDTHC);
    [self.HeaderView addSubview:image];
    [self getButton];
    self.disTableView.tableHeaderView = self.HeaderView;
   
    
    
   
    
    
}

-(void)getData{
    
    
    [SSNetworking ssGETWithURL:@"http://open.qyer.com/qyer/footprint/continent_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88250864194671&lon=121.5393777429208&oauth_token=51c16ac42c049d7967cec9579b89a89d&page=1&track_app_channel=App%2520Store&track_app_version=6.8.5&track_device_info=iPhone%25205s&track_deviceid=CBF73BD2-C65E-4BC1-9574-89BDE1EE9C36&track_os=ios%25209.2.1&track_user_id=7664574&v=1" parameter:nil success:^(id result) {
        
        [[YNDataReading shareYNataReading]writeCachePlist:@"destination" result:result];
        
        NSDictionary *dic = [[YNDataReading shareYNataReading] readCachePlist:@"destination"];
    NSArray *dataArray = [dic objectForKey:@"data"];
        
        for (NSDictionary *countryDic in dataArray) {
            DestinationModel *desModel = [[DestinationModel alloc] init];
            [desModel setValuesForKeysWithDictionary:countryDic];
            
            [self.dataArray addObject:desModel];
            
        }
        
        
        //默认亚洲
        DestinationModel *Asia = [self.dataArray objectAtIndex:0];
        self.tempHotArray = [NSMutableArray arrayWithArray:Asia.hot_country];
        self.tempArray = [NSMutableArray arrayWithArray:Asia.country];
        
        
        
        
        [self.disTableView reloadData];
//隐藏
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        
        
    } failure:^(NSError *error) {
        
        
    } network:^(BOOL networking) {
        
        
        NSDictionary *dic = [[YNDataReading shareYNataReading] readCachePlist:@"destination"];
        NSArray *dataArray = [dic objectForKey:@"data"];
        
        for (NSDictionary *countryDic in dataArray) {
            DestinationModel *desModel = [[DestinationModel alloc] init];
            [desModel setValuesForKeysWithDictionary:countryDic];
            
            [self.dataArray addObject:desModel];
            
        }
        
        DestinationModel *Asia = [self.dataArray objectAtIndex:0];
        self.tempHotArray = [NSMutableArray arrayWithArray:Asia.hot_country];
        self.tempArray = [NSMutableArray arrayWithArray:Asia.country];
        
        
        
        
        [self.disTableView reloadData];
//隐藏
       [MBProgressHUD hideHUDForView:self.view animated:YES];
     
        
    }];
    
  }


//改变分区颜色
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // Background color
    
    view.dk_tintColorPicker = DKColorPickerWithRGB(0xF5F5F5,0x363636);
    // Text Color
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    
    [header.textLabel setTextColor:[UIColor blackColor]];
    
}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
        if (section == 0) {
        return @"热门城市";
    }
    if (self.tempHotArray.count == 1) {
        return nil;
    }
    return @"其他城市";
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 2;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        if(self.tempHotArray.count == 1){
            return 300 * WIDTHC;
        }
        return 1100 * WIDTHC;
    }
    return 50 * WIDTHC;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 1;
    }
    return self.tempArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    

    
    if (indexPath.section == 0) {
        HotCountryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HotCountryCell];
        cell.destinationArray = self.tempHotArray;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        __block DestinationViewController *hotCountry = self;
        
        [cell setHotCountryBlock:^(NSNumber *countryId) {
            DestinstionDetailsViewController *details = [[DestinstionDetailsViewController alloc] init];
            
                details.string = countryId;
            
            NSInteger integer = [countryId integerValue];
            
            if (integer == 62||integer == 10723||integer == 10239||integer == 7789 )  {
                
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"数据维护!" message:nil preferredStyle:UIAlertControllerStyleAlert];

                [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
                 [self presentViewController:alert animated:YES completion:nil];
                
            }else{
                
            [hotCountry presentViewController:details animated:YES completion:^{
            }];
            }
        }];
          
    return cell;
    }
    CountryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:countryCell];
    countryModel *country = [self.tempArray objectAtIndex:indexPath.row];
    cell.country = country;
    return cell;
    
}
//点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DestinstionDetailsViewController *details = [[DestinstionDetailsViewController alloc] init];
    if (indexPath.section == 1) {
        countryModel *country = [self.tempArray objectAtIndex:indexPath.row];
        details.string = country.countryId;
     NSInteger integer = [country.countryId integerValue];
        if (integer == 62||integer == 10723||integer == 10239||integer == 7789 )  {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"数据维护!" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
            [self presentViewController:alert animated:YES completion:nil];
            
        }else{
            
    
        
    [self presentViewController:details animated:YES completion:^{
        
    }];
            
     }
  }
}

-(void)getButton{
    UIButton *AsiaButton = [UIButton buttonWithType:UIButtonTypeCustom];
    AsiaButton.frame = CGRectMake(300 * WIDTHC, 70 * WIDTHC, 50 * WIDTHC, 30 * WIDTHC);
    [AsiaButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
    [AsiaButton setTitle:@"亚洲" forState: UIControlStateNormal];
    AsiaButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [AsiaButton setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
    [self.HeaderView addSubview:AsiaButton];
    [AsiaButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    AsiaButton.tag = 1;
    
    
    UIButton *EuropeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    EuropeButton.frame = CGRectMake(200 * WIDTHC, 50 * WIDTHC, 50 *WIDTHC, 30 *WIDTHC);
    [EuropeButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
    [EuropeButton setTitle:@"欧洲" forState: UIControlStateNormal];
    EuropeButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [EuropeButton setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
    [self.HeaderView addSubview:EuropeButton];
    [EuropeButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    EuropeButton.tag = 2;
    
    
    UIButton *NorthAButton = [UIButton buttonWithType:UIButtonTypeCustom];
    NorthAButton.frame = CGRectMake(60 * WIDTHC, 40 * WIDTHC, 50 * WIDTHC, 30 * WIDTHC);
    [NorthAButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
    [NorthAButton setTitle:@"北美洲" forState: UIControlStateNormal];
    NorthAButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [NorthAButton setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
    [self.HeaderView addSubview:NorthAButton];
    [NorthAButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    NorthAButton.tag = 3;
    
    
    UIButton *SouthAButton = [UIButton buttonWithType:UIButtonTypeCustom];
    SouthAButton.frame = CGRectMake(100 * WIDTHC, 120 * WIDTHC, 50 * WIDTHC,WIDTHC *  30);
    [SouthAButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
    [SouthAButton setTitle:@"南美洲" forState: UIControlStateNormal];
    SouthAButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [SouthAButton setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
    [self.HeaderView addSubview:SouthAButton];
    [SouthAButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    SouthAButton.tag = 4;
    
    UIButton *OceaniaButton = [UIButton buttonWithType:UIButtonTypeCustom];
    OceaniaButton.frame = CGRectMake(320 * WIDTHC, 140 * WIDTHC, 50 * WIDTHC, 30 * WIDTHC);
    [OceaniaButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
    [OceaniaButton setTitle:@"大洋洲" forState: UIControlStateNormal];
    OceaniaButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [OceaniaButton setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
    [self.HeaderView addSubview:OceaniaButton];
    [OceaniaButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    OceaniaButton.tag = 5;
    
    
    UIButton *AfricaButton = [UIButton buttonWithType:UIButtonTypeCustom];
    AfricaButton.frame = CGRectMake(200 * WIDTHC, 100 * WIDTHC, 50 * WIDTHC, 30 * WIDTHC);
    [AfricaButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
    [AfricaButton setTitle:@"非洲" forState: UIControlStateNormal];
    AfricaButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [AfricaButton setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
    [self.HeaderView addSubview:AfricaButton];
    [AfricaButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    AfricaButton.tag = 6;
    
    UIButton *AntarcticaButton = [UIButton buttonWithType:UIButtonTypeCustom];
    AntarcticaButton.frame = CGRectMake(200 * WIDTHC, 220 * WIDTHC, 50 * WIDTHC, 30 * WIDTHC);
     [AntarcticaButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];
     [AntarcticaButton setTitle:@"南极洲" forState: UIControlStateNormal];
     AntarcticaButton.titleLabel.font = [UIFont systemFontOfSize:15];
     [AntarcticaButton setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
    [self.HeaderView addSubview:AntarcticaButton];
    [AntarcticaButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    AntarcticaButton.tag = 7;
}

-(void)buttonAction:(UIButton *)button{
    if (self.dataArray.count == 0) {
        DestinationModel *Asia = [self.readArray objectAtIndex:0];
        DestinationModel *Europe = [self.readArray objectAtIndex:1];
        DestinationModel *NorthAmerica = [self.readArray objectAtIndex:2];
        DestinationModel *SouthAmerica = [self.readArray objectAtIndex:3];
        DestinationModel *Oceania = [self.readArray objectAtIndex:4];
        DestinationModel *Africa = [self.readArray objectAtIndex:5];
        DestinationModel *Antarctica = [self.readArray objectAtIndex:6];
    
        switch (button.tag) {
            case 1:
                self.tempHotArray = [NSMutableArray arrayWithArray:Asia.hot_country];
                self.tempArray = [NSMutableArray arrayWithArray:Asia.country];
                [self.disTableView reloadData];
                break;
            case 2:
                self.tempHotArray = [NSMutableArray arrayWithArray:Europe.hot_country];
                self.tempArray = [NSMutableArray arrayWithArray:Europe.country];
                [self.disTableView reloadData];
                break;
            case 3:
                self.tempHotArray = [NSMutableArray arrayWithArray:NorthAmerica.hot_country];
                self.tempArray = [NSMutableArray arrayWithArray:NorthAmerica.country];
                [self.disTableView reloadData];
                break;
            case 4:
                self.tempHotArray = [NSMutableArray arrayWithArray:SouthAmerica.hot_country];
                self.tempArray = [NSMutableArray arrayWithArray:SouthAmerica.country];
                [self.disTableView reloadData];
                break;
            case 5:
                self.tempHotArray = [NSMutableArray arrayWithArray:Oceania.hot_country];
                self.tempArray = [NSMutableArray arrayWithArray:Oceania.country];
                [self.disTableView reloadData];
                break;
            case 6:
                self.tempHotArray = [NSMutableArray arrayWithArray:Africa.hot_country];
                self.tempArray = [NSMutableArray arrayWithArray:Africa.country];
                [self.disTableView reloadData];
                break;
            default:
                self.tempHotArray = [NSMutableArray arrayWithArray:Antarctica.hot_country];
                self.tempArray = [NSMutableArray arrayWithArray:Antarctica.country];
                [self.disTableView reloadData];
                break;
        }
  
        
        
        
    }else{
    
    DestinationModel *Asia = [self.dataArray objectAtIndex:0];
    DestinationModel *Europe = [self.dataArray objectAtIndex:1];
    DestinationModel *NorthAmerica = [self.dataArray objectAtIndex:2];
    DestinationModel *SouthAmerica = [self.dataArray objectAtIndex:3];
    DestinationModel *Oceania = [self.dataArray objectAtIndex:4];
    DestinationModel *Africa = [self.dataArray objectAtIndex:5];
    DestinationModel *Antarctica = [self.dataArray objectAtIndex:6];
    
    
     switch (button.tag) {
        case 1:
             self.tempHotArray = [NSMutableArray arrayWithArray:Asia.hot_country];
             self.tempArray = [NSMutableArray arrayWithArray:Asia.country];
             [self.disTableView reloadData];
            break;
        case 2:
             self.tempHotArray = [NSMutableArray arrayWithArray:Europe.hot_country];
             self.tempArray = [NSMutableArray arrayWithArray:Europe.country];
             [self.disTableView reloadData];
            break;
        case 3:
             self.tempHotArray = [NSMutableArray arrayWithArray:NorthAmerica.hot_country];
             self.tempArray = [NSMutableArray arrayWithArray:NorthAmerica.country];
             [self.disTableView reloadData];
            break;
        case 4:
             self.tempHotArray = [NSMutableArray arrayWithArray:SouthAmerica.hot_country];
             self.tempArray = [NSMutableArray arrayWithArray:SouthAmerica.country];
             [self.disTableView reloadData];
            break;
        case 5:
             self.tempHotArray = [NSMutableArray arrayWithArray:Oceania.hot_country];
             self.tempArray = [NSMutableArray arrayWithArray:Oceania.country];
             [self.disTableView reloadData];
            break;
        case 6:
             self.tempHotArray = [NSMutableArray arrayWithArray:Africa.hot_country];
             self.tempArray = [NSMutableArray arrayWithArray:Africa.country];
             [self.disTableView reloadData];
            break;
        default:
             self.tempHotArray = [NSMutableArray arrayWithArray:Antarctica.hot_country];
             self.tempArray = [NSMutableArray arrayWithArray:Antarctica.country];
             [self.disTableView reloadData];
            break;
    }
   }
}


//section跟tableView一起滚动的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.disTableView)
    {
        CGFloat sectionHeaderHeight = 50;
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0)
        {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y,0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight)
        {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}

@end
